﻿using EasyBuyShop.Models;
using EasyBuyShop.Utils;

namespace EasyBuyShop.DAL
{
    public class ProductDal:BaseDal
    {
        public List<Product> GetProductList(int categoryId,int subCategoryId,string productName)
        {
            try
            {
                using (var db = this.GetDb(BaseDal.ConnStr))
                {
                    if(categoryId>0 && subCategoryId > 0)
                    {
                        return db.Queryable<Product>().Where(r=>r.CategoryId==categoryId && r.SubCategoryId==subCategoryId).ToList();
                    }
                    else
                    {
                        return db.Queryable<Product>().Where(r => r.Name.Contains(productName)).ToList();
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Fatal(ex.Message);
                return new List<Product>();
            }
        }

        /// <summary>
        /// 新品前100
        /// </summary>
        /// <returns></returns>
        public List<Product> GetTopNew100()
        {
            try
            {
                using (var db = this.GetDb(BaseDal.ConnStr))
                {
                    return db.Queryable<Product>().OrderByDescending(r => r.Id).Take(100).ToList();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Fatal(ex.Message);
                return new List<Product>();
            }
        }

        /// <summary>
        /// 折扣Top3
        /// </summary>
        /// <returns></returns>
        public List<Product> GetTopDisCount3()
        {
            try
            {
                using (var db = this.GetDb(BaseDal.ConnStr))
                {
                    return db.Queryable<Product>().OrderByDescending(r => r.Preferential).Take(3).ToList();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Fatal(ex.Message);
                return new List<Product>();
            }
        }

        public Product GetProduct(long Id)
        {
            try
            {
                using (var db = this.GetDb(BaseDal.ConnStr))
                {
                    return db.Queryable<Product>().First(r=>r.Id==Id);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Fatal(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 获取ID列表对应的产品
        /// </summary>
        /// <param name="productds"></param>
        /// <returns></returns>
        public List<Product> GetProductListByIds(List<long> productds)
        {
            try
            {
                using (var db = this.GetDb(BaseDal.ConnStr))
                {
                    return db.Queryable<Product>().Where(r=>productds.Contains(r.Id)).ToList();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Fatal(ex.Message);
                return new List<Product>();
            }
        }
    }
}
